<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:24 
 * 类说明
 */
class SchoolAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	// 新增
	public function add(){
		$ProvinceModel = M('ZhProvince');$CityModel = M('ZhCity');$AreaModel = M('ZhArea');
		$province = $ProvinceModel->select();
		$city = $CityModel->where("province_id=".$province[0][id])->select();
		$area = $AreaModel->where("city_id=".$city[0][id])->select();
		$this->assign("province",$province);
		$this->assign("city",$city);
		$this->assign("area",$area);
		$this->display();
	}
	
	/**
	 * 获取城市
	 */
	public function getCity(){
		$CityModel = M('ZhCity');$AreaModel = M('ZhArea');
		$province = $_REQUEST["province"];
		$city = $CityModel->where("province_id=".$province)->select();
		$area = $AreaModel->where("city_id=".$city[0][id])->select();
		$vo[0] = $city;
		$vo[1] = $area;
		$this->ajaxReturn($vo,1,0);
	}
	
	/**
	 * 获取区
	 */
	public function getArea(){
		$AreaModel = M('ZhArea');
		$city = $_REQUEST["city"];
		$vo = $AreaModel->where("city_id=".$city)->select();
		$this->ajaxReturn($vo,1,0);
	}
	
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$map['flag'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	
	
	//修改
	public function edit() {
		$Model = M($this->getActionName());$ProvinceModel = M('ZhProvince');$CityModel = M('ZhCity');$AreaModel = M('ZhArea');
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		//取得当前省市区编码
		$province_code = $ProvinceModel->where("province='".$vo['province']."'")->getField("id");
		$city_code = $CityModel->where("city='".$vo['city']."'")->getField("id");
		$area_code = $AreaModel->where("area='".$vo['area']."'")->getField("id");
		$vo['province_code'] = $province_code;
		$vo['city_code'] = $city_code;
		$vo['area_code'] = $area_code;
		//获取列表
		$province_list = $ProvinceModel->select();
		$city_list = $CityModel->where("province_id=".$province_code)->select();
		$area_list = $AreaModel->where("city_id=".$city_code)->select();
		$this->assign('province',$province_list);
		$this->assign('city',$city_list);
		$this->assign('area',$area_list);
		$this->assign('vo',$vo);
		$this->display();
	}
	
	/**
	 * 更新
	 */
	public function update() {
		$Model = D($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	/**
	 * 逻辑删除
	 */
	public function logicDel(){
		$Model = M($this->getActionName());
		$AddressModel = M("Address");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$map['flag'] = 1;
				$map['school_id'] = $id;
				$count = $AddressModel->where($map)->count();
				if($count != 0){
					$this->ajaxReturn(0,"删除失败！该学校已有用户绑定！",0);
				}
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->deleteSession();
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}
	
	/**
	 * @authority 修改排序
	 */
	public function editSeq() {
		$Model = D($this->getActionName());
		$map['flag'] = 1;
		$data = $Model->where($map)->order('seq asc')->field('id,name as name')->select();
		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "学校";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}
		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}
	
	/**
	 * @authority 更新排序
	 */
	public function updateSeq(){
		$Model = D($this->getActionName());
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = "";
		$sql .= 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}
	
	
	//检查学校名是否可注册
	public function checkName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		$where['name'] = $param;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	//检查学校名是否可注册，排除当前
	public function exCheckName(){
		$param = $_REQUEST['param'];
		$user = M($this->getActionName());
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$result = $user->where($map)->find();
			if($param == $result['name']) {
				echo 'y';
				return;
			}else{
				$where['name'] = $param;
				$where['flag'] = 1;
			}
		}else{
			$where['name'] = $param;
			$where['flag'] = 1;
		}
		$count=$user->where($where)->count();
		if($count){
			echo '学校名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	/**
	 * 更改状态
	 */
	public function updateStatus() {
		//删除指定记录
		$Model = M($this->getActionName());
		$pk = $Model->getPk();
		$id = $_REQUEST[$pk];
		if (isset($id)) {
			$condition = array($pk => array('in', explode(',', $id)));
			if (false !== $Model->where($condition)->setField($_REQUEST['field'],$_REQUEST['islock'])) {
				$this->deleteSession();
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}
	
	private function deleteSession(){
		S('S_SYSTEM_SCHOOL',null);
		S('S_SCHOOL',null);
	}

	public function schoolAreas(){
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		$this->display();
	}

	public function schoolAreaList(){
		$SareasModel = M('SchoolAreas');
		$SchoolModel = M('School');
		$ShopModel = M('Shop');
		$map['school_id'] = $_REQUEST['id'];
		$map['flag'] = 1;
		$total = $SareasModel->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $SareasModel->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$School = $SchoolModel->where(array('id'=>$map['school_id']))->field("name")->find();
		for($i=0;$i<count($data);$i++){
			$shopCount = $ShopModel->where("school_area_id=".$data[$i]['id'])->count();
			if($shopCount){
				$data[$i]['shopCount'] = $shopCount;
			}else{
				$data[$i]['shopCount'] = "无";
			}
			$data[$i]['school'] = $School['name'];
		}
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	// 新增学校区域
	public function addSchoolArea(){
		$id = $_REQUEST['id'];
		$this->assign("school_id",$id);
		$this->display();
	}
	//执行新增学校区域
	public function insertSchoolArea(){
		$Model = M('SchoolAreas');
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$map['flag'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}

	/**
	 * @authority 修改学校区域排序
	 */
	public function editSchoolAreaSeq() {
		$Model =  M('SchoolAreas');
		$map['flag'] = 1;
		$data = $Model->where($map)->order('seq asc')->field('id,area_name as name')->select();
		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "学校区域";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}
		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}

	/**
	 * @authority 更新学校区域排序
	 */
	public function updateSchoolAreaSeq(){
		$Model = D("SchoolAreas");
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = "";
		$sql .= 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}

	/**
	 * 更改学校区域状态
	 */
	public function updateSchoolAreaStatus() {
		//删除指定记录
		$Model = M("SchoolAreas");
		$id = $_REQUEST['id'];
		if (isset($id)) {
			if (false !== $Model->where(array('id'=>$id))->setField($_REQUEST['field'],$_REQUEST['status'])) {
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}

	//修改学校区域
	public function editSchoolArea() {
		$Model = M("SchoolAreas");
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}

	/**
	 * 更新学校区域
	 */
	public function updateSchoolArea() {
		$Model = D("SchoolAreas");
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->deleteSession();
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}

	/**
	 * 逻辑删除
	 */
	public function logicDelSchoolAreas(){
		$Model = M("SchoolAreas");
		$shopModel = M("Shop");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$map['flag'] = 1;
				$map['school_area_id'] = $id;
				$count = $shopModel->where($map)->count();
				if($count != 0){
					$this->ajaxReturn(0,"删除失败！该区域已有用户开店！",0);
				}
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->deleteSession();
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}

	//检查学校名是否可注册
	public function checkSchoolAreaName(){
		$area_name = $_REQUEST['param'];
		$school_id = $_REQUEST['school_id'];
		$user = M("SchoolAreas");
		$where['school_id'] = $school_id;
		$where['area_name'] = $area_name;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo '学校区域名已被注册！';
		}else{
			echo 'y';
		}
	}

	//检查学校名是否可注册，排除当前
	public function exCheckSchoolAreaName(){
		$param = $_REQUEST['param'];
		$SchoolArea = M("SchoolAreas");
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$map['school_id'] = $_REQUEST['school_id'];
			$result = $SchoolArea->where($map)->find();
			if($param == $result['area_name']) {
				echo 'y';
				return;
			}else{
				$where['school_id'] = $_REQUEST['school_id'];
				$where['area_name'] = $param;
				$where['flag'] = 1;
			}
		}else{
			$where['school_id'] = $_REQUEST['school_id'];
			$where['area_name'] = $param;
			$where['flag'] = 1;
		}
		$count=$SchoolArea->where($where)->count();
		if($count){
			echo '学校区域名已被注册！';
		}else{
			echo 'y';
		}
	}

	/*
	 * 学校大楼
	 */
	public function schoolFloor(){
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		$this->display();
	}
	/*
	 * 学校大楼列表
	 */
	public function schoolFloorList(){
		$SchoolFloorModel = M('SchoolFloor');
		$SchoolModel = M('School');
		$map['school_id'] = $_REQUEST['id'];
		$map['flag'] = 1;
		$total = $SchoolFloorModel->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $SchoolFloorModel->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$School = $SchoolModel->where(array('id'=>$map['school_id']))->field("name")->find();
		for($i=0;$i<count($data);$i++){
			$data[$i]['school'] = $School['name'];
		}
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}

	// 新增学校大楼
	public function addSchoolFloor(){
		$id = $_REQUEST['id'];
		$this->assign("school_id",$id);
		$this->display();
	}

	//执行新增学校区域
	public function insertSchoolFloor(){
		$Model = M('SchoolFloor');
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		$map['flag'] = 1;
		$max_seq = $Model->where($map)->order('seq desc')->getField('seq');
		$data['seq'] = $max_seq + 1;
		//保存当前数据对象
		if ($result = $Model->add($data)) { //保存成功
			$this->ajaxReturn(1,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}

	/**
	 * @authority 修改学校区域排序
	 */
	public function editSchoolFloorSeq() {
		$Model =  M('SchoolFloor');
		$map['flag'] = 1;
		$data = $Model->where($map)->order('seq asc')->field('id,floor_num as name')->select();
		$newData = array();
		$newData[0]['id'] = -1;
		$newData[0]['pId'] = 0;
		$newData[0]['name'] = "学校大楼";
		$newData[0]['isParent'] = true;
		$newData[0]['open'] = true;
		for($i=0; $i<count($data); $i++) {
			$newData[$i+1]['id'] = $data[$i]['id'];
			$newData[$i+1]['pId'] = $newData[0]['id'];
			$newData[$i+1]['name'] = $data[$i]['name'];
		}
		$this->assign("zNodes",json_encode($newData));
		$this->display();
	}

	/**
	 * @authority 更新学校区域排序
	 */
	public function updateSchoolFloorSeq(){
		$Model = D("SchoolFloor");
		//批量更新
		$seq = $_REQUEST['seq'];
		$sql = "";
		$sql .= 'SET seq = CASE id ';
		$ids = "";
		foreach ($seq as $key=>$val){
			$ids .= $val.",";
			$key++;
			$sql .= ' WHEN '.$val.' THEN '.$key." ";
		}
		$sql .= ' END ';
		$ids = substr($ids,0,strlen($ids)-1);
		$sql .= 'where id in ('.$ids.')';
		$where['id'] = array('in',$ids);
		$data['_query'] = $sql;
		$result = $Model->execute('update __TABLE__ '.$sql);
		if($result !== false){
			$this->ajaxReturn(1,L('ADMIN_SEQ_SUCCESS'),1);
		}else{
			$this->ajaxReturn(0,L('ADMIN_SEQ_ERROR'),0);
		}
	}

	/**
	 * 更改学校区域状态
	 */
	public function updateSchoolFloorStatus() {
		//删除指定记录
		$Model = M("SchoolFloor");
		$id = $_REQUEST['id'];
		if (isset($id)) {
			if (false !== $Model->where(array('id'=>$id))->setField($_REQUEST['field'],$_REQUEST['status'])) {
				$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
			} else {
				$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
			}
		}
	}

	//修改学校区域
	public function editSchoolFloor() {
		$Model = M("SchoolFloor");
		$map['id'] = $_REQUEST['id'];
		$vo = $Model->where($map)->find();
		$this->assign('vo',$vo);
		$this->display();
	}

	/**
	 * 更新学校区域
	 */
	public function updateSchoolFloor() {
		$Model = D("SchoolFloor");
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		// 更新数据
		if(false !== $Model->save()) {
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}

	/**
	 * 逻辑删除
	 */
	public function logicDelSchoolFloor(){
		$Model = M("SchoolFloor");
		$shopModel = M("Shop");
		if (!empty($Model)) {
			$pk = $Model->getPk();
			$id = $_REQUEST[$pk];
			if (isset($id)) {
				$condition = array($pk => array('in', explode(',', $id)));
				if (false !== $Model->where($condition)->setField('flag',2)) {
					$this->ajaxReturn(1,L('ADMIN_DELETE_SUCCESS'),1);
				} else {
					$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
				}
			} else {
				$this->ajaxReturn(0,L('ADMIN_DELETE_ERROR'),0);
			}
		}
	}

	//检查学校名是否可注册
	public function checkSchoolFloorName(){
		$floor_num = $_REQUEST['param'];
		$school_id = $_REQUEST['school_id'];
		$user = M("SchoolFloor");
		$where['school_id'] = $school_id;
		$where['floor_num'] = $floor_num;
		$where['flag'] = 1;
		$count=$user->where($where)->count();
		if($count){
			echo '学校大楼已存在！';
		}else{
			echo 'y';
		}
	}

	//检查学校名是否可注册，排除当前
	public function exCheckSchoolFloorName(){
		$param = $_REQUEST['param'];
		$SchoolFloor = M("SchoolFloor");
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$map['school_id'] = $_REQUEST['school_id'];
			$result = $SchoolFloor->where($map)->find();
			if($param == $result['floor_num']) {
				echo 'y';
				return;
			}else{
				$where['school_id'] = $_REQUEST['school_id'];
				$where['floor_num'] = $param;
				$where['flag'] = 1;
			}
		}else{
			$where['school_id'] = $_REQUEST['school_id'];
			$where['floor_num'] = $param;
			$where['flag'] = 1;
		}
		$count=$SchoolFloor->where($where)->count();
		if($count){
			echo '学校区域名已被注册！';
		}else{
			echo 'y';
		}
	}

}